<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport">
    <title>绑定中心</title>
    <link rel="stylesheet" th:href="@{/frame/vendors/mdi/css/materialdesignicons.min.css}">
    <link rel="stylesheet" th:href="@{/frame/vendors/base/vendor.bundle.base.css}">
    <link rel="stylesheet" th:href="@{/css/style.css}">
    <link rel="shortcut icon" th:href="@{/images/favicon.png}"/>
</head>

<body>
<div class="container-scroller">


    <nav class="navbar col-lg-12 col-12 p-0 fixed-top d-flex flex-row">
        <div class="navbar-brand-wrapper d-flex justify-content-center">
            <div class="navbar-brand-inner-wrapper d-flex justify-content-between align-items-center w-100">
                <a class="navbar-brand brand-logo" th:href="@{/index}">
                    <h1>
                        <span class="display-4" th:text="${session.user.getUserName()}+'欢迎!'"></span>
                    </h1>
                </a>
                <button class="navbar-toggler navbar-toggler align-self-center" data-toggle="minimize" type="button">
                    <span class="mdi mdi-sort-variant"></span>
                </button>
            </div>
        </div>
        <div class="navbar-menu-wrapper d-flex align-items-center justify-content-end">
            <ul class="navbar-nav navbar-nav-right">
                <li class="nav-item nav-profile dropdown">
                    <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" id="profileDropdown">
                        <img alt="profile" th:src="@{/img/head.jpg}">
                        <span class="nav-profile-name" th:text="${session.user.getUserName()}"></span>
                    </a>
                    <div aria-labelledby="profileDropdown" class="dropdown-menu dropdown-menu-right navbar-dropdown">
                        <a class="dropdown-item" th:href="@{/user/setting}">
                            <i class="mdi mdi-settings text-primary"></i>
                            设置
                        </a>
                        <a class="dropdown-item" th:href="@{/loginout}">
                            <i class="mdi mdi-logout text-primary"></i>
                            登出
                        </a>
                    </div>
                </li>
            </ul>
            <button class="navbar-toggler navbar-toggler-right d-lg-none align-self-center" data-toggle="offcanvas"
                    type="button">
                <span class="mdi mdi-menu"></span>
            </button>
        </div>
    </nav>
    <div class="container-fluid page-body-wrapper">
        <nav class="sidebar sidebar-offcanvas" id="sidebar">
            <ul class="nav">
                <li class="nav-item">
                    <a class="nav-link" th:href="@{/index}">
                        <i class="mdi mdi-home menu-icon"></i>
                        <span class="menu-title">主页</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" th:href="@{/user/bindcenter}">
                        <i class="mdi mdi-view-headline menu-icon"></i>
                        <span class="menu-title">设备绑定中心</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" th:href="@{/user/cars}">
                        <i class="mdi mdi-grid-large menu-icon"></i>
                        <span class="menu-title">设备中心</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" th:href="@{/user/setting}">
                        <i class="mdi mdi-account menu-icon"></i>
                        <span class="menu-title">用户中心</span>
                    </a>
                </li>
            </ul>
        </nav>
        <!-- partial -->
        <div class="main-panel">
            <div class="content-wrapper">
                <div class="row">
                    <div class="col-md-6 grid-margin stretch-card">
                        <div class="card">
                            <div class="card-body">
                                <h4 class="card-title">绑定车辆信息</h4>
                                <p class="card-description">
                                    请认真填写车辆信息，将会对以后救援、分析工作相关联
                                </p>
                                <form id="car-bind" onsubmit="return false;">
                                    <div class="form-group">
                                        <label for="car-name">姓名</label>
                                        <input class="form-control" disabled id="car-name"
                                               th:placeholder="${session.user.getUserName()}" type="text">
                                    </div>
                                    <div class="form-group">
                                        <label for="car-phone">手机号</label>
                                        <input class="form-control" disabled
                                               id="car-phone"
                                               th:placeholder="${session.user.getPhone()}" type="text">
                                    </div>
                                    <div class="form-group">
                                        <label for="car-emergencyPhone">紧急联系人电话</label>
                                        <input class="form-control" disabled
                                               id="car-emergencyPhone"
                                               th:placeholder="${session.user.getEmergencyPhone()}"
                                               type="password">
                                    </div>

                                    <div class="form-group">
                                        <label for="car-LicensePlate">车牌号及车辆类型</label>

                                        <div class="row" style="margin-left: 0px;">
                                            <select class="btn btn-sm btn-outline-primary" name="type"
                                                    style="width: 35%!important;" title="选择车辆类型">
                                                <option th:each="carType:${allCarType}"
                                                        th:text="${carType.getName()}"
                                                        th:value="${carType.getId()}">
                                                </option>
                                            </select>
                                            <input class="form-control" id="car-LicensePlate"
                                                   name="licensePlate" placeholder="请输入车牌号"
                                                   style="width: 60%!important;"
                                                   type="text">
                                        </div>
                                    </div>
                                    <button class="btn btn-primary mr-2" id="car-submit">提交</button>
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6 grid-margin stretch-card">
                        <div class="card">
                            <div class="card-body">
                                <h4 class="card-title">绑定设备信息</h4>
                                <p class="card-description">
                                    请认真填写设备信息，将会对以后救援、分析工作相关联
                                </p>
                                <form id="iot-bind" onsubmit="return false;">
                                    <div class="form-group">
                                        <label for="car-LicensePlate">选择绑定到的车辆</label>
                                        <div class="row" style="width: 100%!important;margin-left: 0px;">
                                            <select autocomplete="off" class="btn btn-sm btn-outline-primary col-12"
                                                    id="id"
                                                    name="CarId" title="选择车牌号">
                                                <option th:disabled="${Car.getId() == -1}"
                                                        th:each="Car:${userAllCars}"
                                                        th:selected="${Car.getId() == -1}"
                                                        th:text="${Car.getLicensePlate()}"
                                                        th:value="${Car.getId()}">请先添加车辆
                                                </option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="car-emergencyPhone">车辆类型</label>
                                        <input class="form-control" disabled
                                               id="iot-type"
                                               type="text">
                                    </div>
                                    <div class="form-group">
                                        <label for="car-name">设备编号</label>
                                        <input class="form-control" id="iot-devid" name="DevId" type="text">
                                    </div>
                                    <div class="form-group">
                                        <label for="car-name">安装位置</label>
                                        <input class="form-control" id="iot-place" name="Place" type="text">
                                    </div>
                                    <button class="btn btn-primary mr-2" id="iot-submit">提交</button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- Plugin js for this page-->
<script th:src="@{/frame/vendors/base/vendor.bundle.base.js}"></script>
<!-- endinject -->
<!-- Plugin js for this page-->
<script th:src="@{/frame/vendors/chart.js/Chart.min.js}"></script>
<script th:src="@{/frame/vendors/datatables.net/jquery.dataTables.js}"></script>
<script th:src="@{/frame/vendors/datatables.net-bs4/dataTables.bootstrap4.js}"></script>
<script th:src="@{/js/off-canvas.js}"></script>
<script th:src="@{/js/hoverable-collapse.js}"></script>
<script th:src="@{/js/template.js}"></script>
<!-- endinject -->
<!-- Custom js for this page-->
<script th:src="@{/js/dashboard.js}"></script>
<script th:src="@{/js/data-table.js}"></script>
<script th:src="@{/js/jquery.dataTables.js}"></script>
<script th:src="@{/js/dataTables.bootstrap4.js}"></script>
<script th:src="@{/layuiadmin/layui/layui.js}"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['layer'], function () {
        var $ = layui.$
            , layer = layui.layer;
        let regLicensePlate = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;

        function ReCheck(element, re, tips) {
            if (!re.test($(element).val())) {
                layer.msg("[提示]" + tips);
                return false;
            }
            return true;
        }

        function LengthCheck(element, minLength, maxLength, tips) {
            let e = $(element).val()
            if (!(e.length >= minLength && e.length <= maxLength)) {
                layer.msg("[提示]" + tips);
                return false;
            }
            return true;
        }

        function getType() {
            let data = $("#id").serialize();
            if ($("#id").val() != null) {
                $.ajax({
                    type: 'post',
                    url: '/data/findCarType',
                    data: data,
                    success: function (Msg) {
                        if (Msg.code != 200) {
                            layer.msg(Msg.msg);
                        } else {
                            $("#iot-type").val(Msg.msg);
                        }
                    }
                })
            }
        }

        $("#iot-devid").blur(() => {
            LengthCheck("#iot-devid", 9, 9, "设备Id为9位整数");
        })
        $("#iot-place").blur(() => {
            LengthCheck("#iot-place", 2, 10, "安装位置一般在2-10字之间");
        })
        $("#iot-submit").click(() => {

            if (!LengthCheck("#iot-devid", 9, 9, "设备Id为9位整数") ||
                !LengthCheck("#iot-place", 2, 10, "安装位置一般在2-10字之间"))
                return;
            let data = decodeURIComponent($("#iot-bind").serialize());
            $.ajax({
                type: 'post',
                url: '/user/bindiot',
                data: data,
                success: function (Msg) {
                    if (Msg.code != 200) {
                        layer.msg(Msg.msg);
                    } else {
                        layer.msg("添加设备成功！");
                        $(':input', '#iot-bind')
                            .not(':button, :submit, :reset, :hidden,select')
                            .val('')
                            .removeAttr('checked')
                            .removeAttr('selected');
                    }
                }
            })
        })
        $("#id").change(
            () => {
                getType();
            }
        )
        getType();
        $("#car-submit").click(() => {

            let data = decodeURIComponent($("#car-bind").serialize(), true);
            if (ReCheck("#car-LicensePlate", regLicensePlate, "车牌号格式不正确！"))
                $.ajax({
                    type: 'post',
                    url: '/user/bindcar',
                    data: data,
                    success: function (Msg) {
                        if (Msg.code != 200) {
                            layer.msg(Msg.msg);
                        } else {
                            layer.msg("添加车辆成功！");
                            $(':input', '#car-bind')
                                .not(':button, :submit, :reset, :hidden,select')
                                .val('')
                                .removeAttr('checked')
                                .removeAttr('selected');
                        }
                    }
                })

        })

        $("#car-LicensePlate").blur(
            () => {
                if (LengthCheck("#car-LicensePlate", 7, 7, "车牌号长度不正确")) {
                    ReCheck("#car-LicensePlate", regLicensePlate, "车牌号格式不正确！")
                }
            }
        )
    })
</script>
</body>

</html>
